@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using System.Collections.Generic
@using System.Linq
using System;
@{
    var namespaceSet = new HashSet<string>(
        new string[]
        {
            "Microsoft.AspNetCore.Identity",
            "Microsoft.AspNetCore.Identity.UI",
            "Microsoft.AspNetCore.Hosting",
            "Microsoft.EntityFrameworkCore",
            "Microsoft.Extensions.Configuration",
            "Microsoft.Extensions.DependencyInjection",
        });
    var thisNamespace = $"{Model.Namespace}.Areas.Identity";

    if (Model.IsGenerateCustomUser)
    {
        if (thisNamespace != Model.UserClassNamespace)
        {
            namespaceSet.Add(Model.UserClassNamespace);
        }
    }
    else
    {
        namespaceSet.Add("Microsoft.AspNetCore.Identity.EntityFrameworkCore");
    }
    if (thisNamespace != Model.DbContextNamespace)
    {
        namespaceSet.Add(Model.DbContextNamespace);
    }
    foreach(var name in namespaceSet.OrderBy(n => n))
    {
@:using @name;
    }
}

[assembly: HostingStartup(typeof(@(thisNamespace).IdentityHostingStartup))]
namespace @(thisNamespace)
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
@{
@:            builder.ConfigureServices((context, services) => {

    if (!Model.IsUsingExistingDbContext)
    {
        if (Model.UseSQLite)
        {
@:                services.AddDbContext<@Model.DbContextClass>(options =>
@:                    options.UseSqlite(
@:                        context.Configuration.GetConnectionString("@(Model.DbContextClass)Connection"),
@:                        sqlOptions => sqlOptions.MigrationsAssembly("@Model.ApplicationName")));
        } // End if Model.UseSQLite
        else
        {
@:                services.AddDbContext<@Model.DbContextClass>(options =>
@:                    options.UseSqlServer(
@:                        context.Configuration.GetConnectionString("@(Model.DbContextClass)Connection"),
@:                        sqlOptions => sqlOptions.MigrationsAssembly("@Model.ApplicationName")));
        } // End else
@:
@:                services.AddIdentity<@(Model.UserClass), IdentityRole>()
@:                   .AddEntityFrameworkStores<@Model.DbContextClass>()
@:                   .AddDefaultUI()
@:                   .AddDefaultTokenProviders();
    } // End Model.IsUsingExstingDbContext

    if (!Model.UseDefaultUI)
    {
@:
@:                services.AddMvc()
@:                    .AddRazorPagesOptions(options =>
@:                    {
@:                        options.AllowAreas = true;
@:                        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
@:                        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
@:                    });
@:
@:                services.ConfigureApplicationCookie(options => 
@:                {
@:                    options.LoginPath = "/Identity/Account/Login";
@:                    options.LogoutPath = "/Identity/Account/Logout";
@:                    options.AccessDeniedPath = "/Identity/Account/AccessDenied";
@:                });
    } // End Model.UseDefaultUI
@:            });
        }
        }
    }
}